package com.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.domain.PageDTO;
import com.mall.common.domain.PageQuery;
import com.mall.common.utils.BeanUtils;
import com.mall.domain.dto.OrderFormDTO;
import com.mall.domain.dto.PayOrderDetail;
import com.mall.domain.po.Item;
import com.mall.domain.po.Order;
import com.mall.domain.po.OrderDetail;
import com.mall.domain.response.Response;
import com.mall.domain.vo.OrderCountVO;
import com.mall.domain.vo.OrderVO;
import com.mall.service.IOrderDetailService;
import com.mall.service.IOrderService;
import com.mall.service.ItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "订单管理接口")
@RestController
@RequestMapping("/orders")
@RequiredArgsConstructor
public class OrderController {
    private final IOrderService orderService;
    private final IOrderDetailService orderDetailService;
    private final ItemService itemService;

    @ApiOperation("查询当前用户订单")
    @GetMapping("myOrders")
    public Response<List<OrderVO>> myOrders(@RequestParam(required = false) Integer status) {
        return orderService.myOrders(status);
    }

    @ApiOperation("根据id查询订单")
    @GetMapping("{id}")
    public OrderVO queryOrderById(@Param ("订单id")@PathVariable("id") Long orderId) {
        return BeanUtils.copyBean(orderService.getById(orderId), OrderVO.class);
    }

    @ApiOperation("创建订单")
    @PostMapping
    public Long createOrder(@RequestBody OrderFormDTO orderFormDTO){
        return orderService.createOrder(orderFormDTO);
    }

    @ApiOperation("标记订单已支付")
    @ApiImplicitParam(name = "orderId", value = "订单id", paramType = "path")
    @PutMapping("/{orderId}")
    public void markOrderPaySuccess(@PathVariable("orderId") Long orderId) {
        orderService.markOrderPaySuccess(orderId);
    }

    /**
     * 计算今日订单数，今日销售额，待发货订单数
     */
    @GetMapping("/dashboard")
    public Response<OrderCountVO> dashboard(){
        return orderService.dashboard();
    }


    /**
     * 商家获取所有订单
     */
    @GetMapping
    public Response<PageDTO<OrderVO>> getOrderList(
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "orderNo", required = false) String orderNo,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate) {

        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNo(pageNo);
        pageQuery.setPageSize(pageSize);
        
        return orderService.getOrderList(pageQuery, orderNo, status, startDate, endDate);
    }
    
    /**
     * 取消订单
     */
    @ApiOperation("取消订单")
    @ApiImplicitParam(name = "orderId", value = "订单id", paramType = "path")
    @PostMapping("/{orderId}/cancel")
    public Response<Boolean> cancelOrder(@PathVariable("orderId") Long orderId) {
        boolean result = orderService.cancelOrder(orderId);
        return Response.success(result);
    }
    
    /**
     * 订单发货
     */
    @ApiOperation("订单发货")
    @PostMapping("/ship")
    public Response<Boolean> shipOrder(@RequestBody Map<String, Object> shipForm) {
        Long orderId = Long.valueOf(shipForm.get("orderId").toString());
        String expressCompany = (String) shipForm.get("expressCompany");
        String trackingNumber = (String) shipForm.get("trackingNumber");
        boolean result = orderService.shipOrder(orderId, expressCompany, trackingNumber);
        return Response.success(result);
    }

    @ApiOperation("获得订单与商品详情")
    @GetMapping("/OrderItemDetail")
    public Response<PayOrderDetail> getItemDetail(Long id){
        QueryWrapper<OrderDetail> orderDetailQueryWrapper = new QueryWrapper<>();
        orderDetailQueryWrapper.eq("order_id",id);

        List<OrderDetail> list = orderDetailService.list(orderDetailQueryWrapper);
        List<Long> itemIds = list.stream().map(OrderDetail::getItemId).toList();
        QueryWrapper<Item> itemQueryWrapper = new QueryWrapper<>();
        itemQueryWrapper.in("id",itemIds);
        List<Item> list1 = itemService.list(itemQueryWrapper);
        Order order = orderService.getById(id);

        PayOrderDetail payOrderDetail = new PayOrderDetail();
        BeanUtils.copyProperties(order,payOrderDetail);
        payOrderDetail.setItems(list1);
        return Response.success(payOrderDetail);
    }
    /**
     * 发货
     */
    @GetMapping("/{orderid}/ship")
    public Response<?> shipping(@PathVariable Long orderid){
        return orderService.shipping(orderid);
    }
}
